package com.ioudi.digu.security.userdetails;

import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;

import com.ioudi.digu.service.IUserService;
import com.ioudi.digu.service.impl.UserService;
import com.ioudi.digu.vo.User;

public class UserDetailService implements UserDetailsService
{
	private IUserService userService;
	
	public UserDetails loadUserByUsername(String username)
		throws UsernameNotFoundException, DataAccessException
	{
		//reject the user login from this channel, a client login only by
		//UserService
		Map map = userService.retrieveUserDetail(username);
		GrantedAuthority[] gas = {new GrantedAuthorityImpl("ROLE_USER")};
		String password = String.valueOf(map.get("credentials"));
		com.ioudi.digu.security.userdetails.UserDetails user = 
			new com.ioudi.digu.security.userdetails.UserDetails(username, password, true, true, true, true, gas);
		User real = map.get("user") instanceof User ? (User)map.get("user") : null;
		user.setRealUser(real);
		return user;
	}
	
	public IUserService getUserService()
	{
		return userService;
	}

	public void setUserService(IUserService userService)
	{
		this.userService = userService;
	}
}
